/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package pw.yumc.Residence.listeners;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Hanging;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerBucketEmptyEvent;
import org.bukkit.event.player.PlayerBucketFillEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
import org.bukkit.plugin.Plugin;

import com.bekvon.bukkit.residence.event.ResidenceChangedEvent;
import com.bekvon.bukkit.residence.permissions.PermissionGroup;
import com.bekvon.bukkit.residence.protection.ClaimedResidence;
import com.bekvon.bukkit.residence.protection.FlagPermissions;
import com.sk89q.worldedit.bukkit.WorldEditPlugin;

import cn.citycraft.PluginHelper.utils.ActionBar;
import cn.citycraft.PluginHelper.utils.CompatibleUtil;
import pw.yumc.Residence.ResidenceMain;
import pw.yumc.Residence.chat.ChatChannel;

/**
 *
 * @author Administrator
 */
@SuppressWarnings("deprecation")
public class ResidencePlayerListener implements Listener {

    protected boolean chatenabled;
    protected Map<String, String> currentRes;
    protected Map<String, Location> lastOutsideLoc;
    protected Map<String, Long> lastUpdate;
    protected int minUpdateTime;
    protected List<String> playerToggleChat;
    ResidenceMain plugin;

    public ResidencePlayerListener(final ResidenceMain plugin) {
        this.plugin = plugin;
        currentRes = new HashMap<>();
        lastUpdate = new HashMap<>();
        lastOutsideLoc = new HashMap<>();
        playerToggleChat = new ArrayList<>();
        minUpdateTime = plugin.getConfigManager().getMinMoveUpdateInterval();
        chatenabled = plugin.getConfigManager().chatEnabled();
        for (final Player player : CompatibleUtil.getOnlinePlayers()) {
            lastUpdate.put(player.getName(), System.currentTimeMillis());
        }
    }

    public String getCurrentResidenceName(final String player) {
        return currentRes.get(player);
    }

    public void handleNewLocation(final Player player, final Location loc, final boolean move) {
        final String pname = player.getName();

        ClaimedResidence res = plugin.getResidenceManager().getByLoc(loc);
        String areaname = null;
        boolean chatchange = false;
        String subzone = null;
        if (res != null) {
            areaname = plugin.getResidenceManager().getNameByLoc(loc);
            while (res.getSubzoneByLoc(player.getLocation()) != null) {
                subzone = res.getSubzoneNameByLoc(player.getLocation());
                res = res.getSubzoneByLoc(player.getLocation());
                areaname = areaname + "." + subzone;
            }
        }
        ClaimedResidence ResOld = null;
        if (currentRes.containsKey(pname)) {
            ResOld = plugin.getResidenceManager().getByName(currentRes.get(pname));
            if (ResOld == null) {
                currentRes.remove(pname);
            }
        }
        if (res == null) {
            lastOutsideLoc.put(pname, loc);
            if (ResOld != null) {
                final String leave = ResOld.getLeaveMessage();
                final ResidenceChangedEvent chgEvent = new ResidenceChangedEvent(ResOld, null, player);
                plugin.getServer().getPluginManager().callEvent(chgEvent);

                if (leave != null && !leave.equals("")) {
                    if (plugin.getConfigManager().useActionBar()) {
                        ActionBar.send(player, ChatColor.YELLOW + insertMessages(player, ResOld.getName(), ResOld, leave));
                    } else {
                        player.sendMessage(ChatColor.YELLOW + insertMessages(player, ResOld.getName(), ResOld, leave));
                    }
                }
                currentRes.remove(pname);
                plugin.getChatManager().removeFromChannel(pname);
            }
            return;
        }
        if (move && !res.getPermissions().playerHas(pname, "move", true) && !plugin.isResAdminOn(player) && !player.hasPermission("residence.admin.move")) {
            final Location lastLoc = lastOutsideLoc.get(pname);
            if (lastLoc != null) {
                player.teleport(lastLoc);
            } else {
                player.teleport(res.getOutsideFreeLoc(loc));
            }
            player.sendMessage(ChatColor.RED + plugin.getLanguage().getPhrase("ResidenceMoveDeny", res.getName().split("\\.")[res.getName().split("\\.").length - 1]));
            return;
        }
        lastOutsideLoc.put(pname, loc);
        if (!currentRes.containsKey(pname) || ResOld != res) {
            currentRes.put(pname, areaname);
            if (subzone == null) {
                chatchange = true;
            }
            ClaimedResidence chgFrom = null;
            if (ResOld != res && ResOld != null) {
                final String leave = ResOld.getLeaveMessage();
                chgFrom = ResOld;

                if (leave != null && !leave.equals("") && ResOld != res.getParent()) {
                    if (plugin.getConfigManager().useActionBar()) {
                        ActionBar.send(player, ChatColor.YELLOW + insertMessages(player, ResOld.getName(), ResOld, leave));
                    } else {
                        player.sendMessage(ChatColor.YELLOW + insertMessages(player, ResOld.getName(), ResOld, leave));
                    }
                }
            }
            final String enterMessage = res.getEnterMessage();
            final ResidenceChangedEvent chgEvent = new ResidenceChangedEvent(chgFrom, res, player);
            plugin.getServer().getPluginManager().callEvent(chgEvent);

            if (enterMessage != null && !enterMessage.equals("") && !(ResOld != null && res == ResOld.getParent())) {
                if (plugin.getConfigManager().useActionBar()) {
                    ActionBar.send(player, ChatColor.YELLOW + insertMessages(player, areaname, res, enterMessage));
                } else {
                    player.sendMessage(ChatColor.YELLOW + insertMessages(player, areaname, res, enterMessage));
                }
            }
        }
        if (chatchange && chatenabled) {
            plugin.getChatManager().setChannel(pname, areaname);
        }
    }

    public String insertMessages(final Player player, final String areaname, final ClaimedResidence res, String message) {
        try {
            message = message.replaceAll("%player", player.getName());
            message = message.replaceAll("%owner", res.getPermissions().getOwner());
            message = message.replaceAll("%residence", areaname);
            message = ChatColor.translateAlternateColorCodes('&', message);
        } catch (final Exception ex) {
            return "";
        }
        return message;
    }

    @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
    public void onPlayerBucketEmpty(final PlayerBucketEmptyEvent event) {
        final Player player = event.getPlayer();
        if (plugin.isResAdminOn(player)) {
            return;
        }
        final String pname = player.getName();
        final ClaimedResidence res = plugin.getResidenceManager().getByLoc(event.getBlockClicked().getLocation());
        if (res != null) {
            if (plugin.getConfigManager().preventRentModify() && plugin.getConfigManager().enabledRentSystem()) {
                if (plugin.getRentManager().isRented(res.getName())) {
                    player.sendMessage(ChatColor.RED + plugin.getLanguage().getPhrase("RentedModifyDeny"));
                    event.setCancelled(true);
                    return;
                }
            }
        }
        final FlagPermissions perms = plugin.getPermsByLocForPlayer(event.getBlockClicked().getLocation(), player);
        if (!perms.playerHas(pname, player.getWorld().getName(), "bucket", perms.playerHas(pname, player.getWorld().getName(), "build", true))) {
            player.sendMessage(ChatColor.RED + plugin.getLanguage().getPhrase("FlagDeny", "bucket"));
            event.setCancelled(true);
        }
    }

    @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
    public void onPlayerBucketFill(final PlayerBucketFillEvent event) {
        final Player player = event.getPlayer();
        final String pname = player.getName();
        if (plugin.isResAdminOn(player)) {
            return;
        }
        final ClaimedResidence res = plugin.getResidenceManager().getByLoc(event.getBlockClicked().getLocation());
        if (res != null) {
            if (plugin.getConfigManager().preventRentModify() && plugin.getConfigManager().enabledRentSystem()) {
                if (plugin.getRentManager().isRented(res.getName())) {
                    player.sendMessage(ChatColor.RED + plugin.getLanguage().getPhrase("RentedModifyDeny"));
                    event.setCancelled(true);
                    return;
                }
            }
        }
        final FlagPermissions perms = plugin.getPermsByLocForPlayer(event.getBlockClicked().getLocation(), player);
        final boolean hasbucket = perms.playerHas(pname, player.getWorld().getName(), "bucket", perms.playerHas(pname, player.getWorld().getName(), "build", true));
        if (!hasbucket) {
            player.sendMessage(ChatColor.RED + plugin.getLanguage().getPhrase("FlagDeny", "bucket"));
            event.setCancelled(true);
        }
    }

    @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
    public void onPlayerChat(final AsyncPlayerChatEvent event) {
        final String pname = event.getPlayer().getName();
        if (chatenabled && playerToggleChat.contains(pname)) {
            final String area = currentRes.get(pname);
            if (area != null) {
                final ChatChannel channel = plugin.getChatManager().getChannel(area);
                if (channel != null) {
                    channel.chat(pname, event.getMessage());
                }
                event.setCancelled(true);
            }
        }
    }

    @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
    public void onPlayerInteract(final PlayerInteractEvent event) {
        final Player player = event.getPlayer();
        final Material heldItem = player.getItemInHand().getType();
        final Block block = event.getClickedBlock();
        if (block == null) {
            return;
        }
        final Material mat = block.getType();
        if (!((isContainer(mat, block) || isCanUseEntity_RClickOnly(mat, block)) && event.getAction() == Action.RIGHT_CLICK_BLOCK || isCanUseEntity_BothClick(mat, block) || event.getAction() == Action.PHYSICAL)) {
            final int typeId = player.getItemInHand().getTypeId();
            if (typeId != plugin.getConfigManager().getSelectionTooldID() && typeId != plugin.getConfigManager().getInfoToolID() && typeId != 351 && typeId != 416) {
                return;
            }
        }
        FlagPermissions perms = plugin.getPermsByLocForPlayer(block.getLocation(), player);
        final String world = player.getWorld().getName();
        final String permgroup = plugin.getPermissionManager().getGroupNameByPlayer(player);
        final boolean resadmin = plugin.isResAdminOn(player);
        if (event.getAction() == Action.PHYSICAL) {
            if (!resadmin) {
                final boolean hasuse = perms.playerHas(player.getName(), world, "use", true);
                final boolean haspressure = perms.playerHas(player.getName(), world, "pressure", hasuse);
                if ((!hasuse && !haspressure || !haspressure) && (mat == Material.STONE_PLATE || mat == Material.WOOD_PLATE)) {
                    event.setCancelled(true);
                    return;
                }
            }
            if (!perms.playerHas(player.getName(), world, "trample", perms.playerHas(player.getName(), world, "build", true)) && (mat == Material.SOIL || mat == Material.SOUL_SAND)) {
                event.setCancelled(true);
                return;
            }
            return;
        }
        if (!resadmin && !plugin.getItemManager().isAllowed(heldItem, permgroup, world)) {
            player.sendMessage(ChatColor.RED + plugin.getLanguage().getPhrase("ItemBlacklisted"));
            event.setCancelled(true);
            return;
        }
        if (event.getAction() == Action.LEFT_CLICK_BLOCK || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
            if (player.getItemInHand().getTypeId() == plugin.getConfigManager().getSelectionTooldID()) {
                if (plugin.getDeny().getStringList("world").contains(player.getWorld().getName()) && !player.isOp()) {
                    player.sendMessage("§c当前世界不允许圈地!");
                    return;
                }
                final Plugin wep = Bukkit.getPluginManager().getPlugin("WorldEdit");
                if (wep != null) {
                    if (((WorldEditPlugin) wep).getConfig().getInt("wand-item") == plugin.getConfigManager().getSelectionTooldID()) {
                        return;
                    }
                }
                final PermissionGroup group = plugin.getPermissionManager().getGroup(player);
                if (player.hasPermission("residence.select")
                        || player.hasPermission("residence.create") && !player.isPermissionSet("residence.select")
                        || group.canCreateResidences() && !player.isPermissionSet("residence.create") && !player.isPermissionSet("residence.select")
                        || resadmin) {
                    if (event.getAction() == Action.LEFT_CLICK_BLOCK) {
                        final Location loc = block.getLocation();
                        plugin.getSelectionManager().placeLoc1(player, loc);
                        player.sendMessage(String.format("§a%s§c(%s,%s,%s)§a!", plugin.getLanguage().getPhrase("SelectPoint", plugin.getLanguage().getPhrase("Primary")), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()));
                        event.setCancelled(true);
                    } else if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
                        final Location loc = block.getLocation();
                        plugin.getSelectionManager().placeLoc2(player, loc);
                        player.sendMessage(String.format("§a%s§c(%s,%s,%s)§a!", plugin.getLanguage().getPhrase("Secondary", plugin.getLanguage().getPhrase("Primary")), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()));
                    }
                }
            }
            if (player.getItemInHand().getTypeId() == plugin.getConfigManager().getInfoToolID()) {
                if (event.getAction() == Action.LEFT_CLICK_BLOCK) {
                    final Location loc = block.getLocation();
                    final String res = plugin.getResidenceManager().getNameByLoc(loc);
                    if (res != null) {
                        plugin.getResidenceManager().printAreaInfo(res, player);
                        event.setCancelled(true);
                    } else {
                        event.setCancelled(true);
                        player.sendMessage(plugin.getLanguage().getPhrase("NoResHere"));
                    }
                }
            }
            if (!resadmin) {
                if (heldItem != null) {
                    if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
                        if (player.getItemInHand().getTypeId() == 351) {
                            if (player.getItemInHand().getData().getData() == 15 && block.getType() == Material.GRASS
                                    || player.getItemInHand().getData().getData() == 3 && block.getTypeId() == 17 && (block.getData() == 3 || block.getData() == 7 || block.getData() == 11 || block.getData() == 15)) {
                                perms = plugin.getPermsByLocForPlayer(block.getRelative(event.getBlockFace()).getLocation(), player);
                                if (!perms.playerHas(player.getName(), world, "build", true)) {
                                    event.setCancelled(true);
                                    return;
                                }
                            }
                        }
                        if (plugin.isGt1_8()) {
                            if (heldItem == Material.ARMOR_STAND) {
                                perms = plugin.getPermsByLocForPlayer(block.getRelative(event.getBlockFace()).getLocation(), player);
                                if (!perms.playerHas(player.getName(), world, "build", true)) {
                                    player.sendMessage(ChatColor.RED + plugin.getLanguage().getPhrase("NoPermission"));
                                    event.setCancelled(true);
                                    return;
                                }
                            }
                        }
                    }
                }
                if (isContainer(mat, block) || isCanUseEntity(mat, block)) {
                    final boolean hasuse = perms.playerHas(player.getName(), world, "use", true);
                    for (final Entry<Material, String> checkMat : FlagPermissions.getMaterialUseFlagList().entrySet()) {
                        if (mat == checkMat.getKey()) {
                            if (!perms.playerHas(player.getName(), world, checkMat.getValue(), hasuse)) {
                                if (hasuse || checkMat.getValue().equals("container")) {
                                    event.setCancelled(true);
                                    player.sendMessage(ChatColor.RED + plugin.getLanguage().getPhrase("FlagDeny", checkMat.getValue()));
                                } else {
                                    event.setCancelled(true);
                                    player.sendMessage(ChatColor.RED + plugin.getLanguage().getPhrase("FlagDeny", "use"));
                                }
                            }
                        }
                    }
                    if (plugin.getConfigManager().getCustomContainers().contains(block.getType())) {
                        if (!perms.playerHas(player.getName(), world, "container", hasuse)) {
                            event.setCancelled(true);
                            player.sendMessage(ChatColor.RED + plugin.getLanguage().getPhrase("FlagDeny", "container"));
                            return;
                        }
                    }
                    if (plugin.getConfigManager().getCustomBothClick().contains(block.getType())) {
                        if (!hasuse) {
                            event.setCancelled(true);
                            player.sendMessage(ChatColor.RED + plugin.getLanguage().getPhrase("FlagDeny", "use"));
                            return;
                        }
                    }
                    if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
                        if (plugin.getConfigManager().getCustomRightClick().contains(block.getType())) {
                            if (!hasuse) {
                                event.setCancelled(true);
                                player.sendMessage(ChatColor.RED + plugin.getLanguage().getPhrase("FlagDeny", "use"));
                            }
                        }
                    }
                }
            }
        }
    }

    @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
    public void onPlayerInteractEntity(final PlayerInteractEntityEvent event) {
        final Player player = event.getPlayer();
        if (plugin.isResAdminOn(player)) {
            return;
        }
        final Entity ent = event.getRightClicked();
        /* Trade */
        if (ent.getType() == EntityType.VILLAGER) {
            final ClaimedResidence res = plugin.getResidenceManager().getByLoc(event.getPlayer().getLocation());

            if (res != null && !res.getPermissions().playerHas(player.getName(), "trade", true)) {
                player.sendMessage(ChatColor.RED + plugin.getLanguage().getPhrase("NoPermission"));
                event.setCancelled(true);
            }
        }
        /* Container - ItemFrame protection */
        final Material heldItem = player.getItemInHand().getType();
        if (!(ent instanceof Hanging)) {
            return;
        }
        final Hanging hanging = (Hanging) ent;
        if (hanging.getType() != EntityType.ITEM_FRAME) {
            return;
        }
        final FlagPermissions perms = plugin.getPermsByLocForPlayer(ent.getLocation(), player);
        final String world = player.getWorld().getName();
        final String permgroup = plugin.getPermissionManager().getGroupNameByPlayer(player);
        if (!plugin.getItemManager().isAllowed(heldItem, permgroup, world)) {
            player.sendMessage(ChatColor.RED + plugin.getLanguage().getPhrase("ItemBlacklisted"));
            event.setCancelled(true);
            return;
        }
        if (!perms.playerHas(player.getName(), world, "container", perms.playerHas(player.getName(), world, "use", true))) {
            event.setCancelled(true);
            player.sendMessage(ChatColor.RED + plugin.getLanguage().getPhrase("FlagDeny", "container"));
        }
    }

    @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
    public void onPlayerJoin(final PlayerJoinEvent event) {
        final Player player = event.getPlayer();
        lastUpdate.put(player.getName(), 0L);
        if (plugin.getPermissionManager().isResidenceAdmin(player)) {
            plugin.turnResAdminOn(player);
        }
        handleNewLocation(player, player.getLocation(), false);
    }

    @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
    public void onPlayerMove(final PlayerMoveEvent event) {
        final Player player = event.getPlayer();
        if (player == null) {
            return;
        }
        final long now = System.currentTimeMillis();
        if (!lastUpdate.containsKey(player.getName())) {
            lastUpdate.put(player.getName(), now);
            return;
        }
        final long last = lastUpdate.get(player.getName());
        if (now - last < plugin.getConfigManager().getMinMoveUpdateInterval()) {
            return;
        }
        lastUpdate.put(player.getName(), now);
        if (event.getFrom().getWorld() == event.getTo().getWorld()) {
            if (event.getFrom().distance(event.getTo()) == 0) {
                return;
            }
        }
        handleNewLocation(player, event.getTo(), true);
    }

    @EventHandler(priority = EventPriority.MONITOR)
    public void onPlayerQuit(final PlayerQuitEvent event) {
        final String pname = event.getPlayer().getName();
        currentRes.remove(pname);
        lastUpdate.remove(pname);
        lastOutsideLoc.remove(pname);
        plugin.getChatManager().removeFromChannel(pname);
    }

    @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
    public void onPlayerSpawn(final PlayerRespawnEvent event) {
        Location loc = event.getRespawnLocation();
        final Boolean bed = event.isBedSpawn();
        final Player player = event.getPlayer();
        ClaimedResidence res = plugin.getResidenceManager().getByLoc(loc);
        if (res == null) {
            return;
        }
        if (res.getPermissions().playerHas(player.getName(), "move", true)) {
            return;
        }
        if (bed) {
            loc = player.getWorld().getSpawnLocation();
        }
        res = plugin.getResidenceManager().getByLoc(loc);
        if (res != null) {
            if (!res.getPermissions().playerHas(player.getName(), "move", true)) {
                loc = res.getOutsideFreeLoc(loc);
            }
        }
        player.sendMessage(ChatColor.RED + plugin.getLanguage().getPhrase("NoSpawn"));
        event.setRespawnLocation(loc);
    }

    @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
    public void onPlayerTeleport(final PlayerTeleportEvent event) {
        final Location loc = event.getTo();
        final Player player = event.getPlayer();
        final ClaimedResidence res = plugin.getResidenceManager().getByLoc(loc);
        if (res != null) {
            if (plugin.isResAdminOn(player)
                    || ((res.getPermissions().playerHas(player.getName(), "tp", true) || player.hasPermission("residence.admin.tp"))
                            && (res.getPermissions().playerHas(player.getName(), "move", true) || player.hasPermission("residence.admin.move")))) {
                handleNewLocation(player, loc, false);
            } else if (event.getCause() != TeleportCause.PLUGIN) {
                final String areaname = res.getName();
                event.setCancelled(true);
                player.sendMessage(ChatColor.RED + plugin.getLanguage().getPhrase("TeleportDeny", areaname));
            }
        }
    }

    public void tooglePlayerResidenceChat(final Player player) {
        final String pname = player.getName();
        if (playerToggleChat.contains(pname)) {
            playerToggleChat.remove(pname);
            player.sendMessage(ChatColor.YELLOW + plugin.getLanguage().getPhrase("ResidenceChat", ChatColor.RED + "OFF" + ChatColor.YELLOW + "!"));
        } else {
            playerToggleChat.add(pname);
            player.sendMessage(ChatColor.YELLOW + plugin.getLanguage().getPhrase("ResidenceChat", ChatColor.RED + "ON" + ChatColor.YELLOW + "!"));
        }
    }

    private boolean isCanUseEntity(final Material mat, final Block block) {
        return isCanUseEntity_BothClick(mat, block) || isCanUseEntity_RClickOnly(mat, block);
    }

    private boolean isCanUseEntity_BothClick(final Material mat, final Block block) {
        return mat == Material.LEVER
                || mat == Material.STONE_BUTTON
                || mat == Material.WOOD_BUTTON
                || mat == Material.WOODEN_DOOR
                || mat == Material.TRAP_DOOR
                || mat == Material.FENCE_GATE
                || mat == Material.PISTON_BASE
                || mat == Material.PISTON_STICKY_BASE
                || mat == Material.DRAGON_EGG
                || plugin.getConfigManager().getCustomBothClick().contains(block.getType())
                || (plugin.isGt1_8()
                        && (mat == Material.SPRUCE_DOOR
                                || mat == Material.BIRCH_DOOR
                                || mat == Material.JUNGLE_DOOR
                                || mat == Material.ACACIA_DOOR
                                || mat == Material.DARK_OAK_DOOR
                                || mat == Material.SPRUCE_FENCE_GATE
                                || mat == Material.BIRCH_FENCE_GATE
                                || mat == Material.JUNGLE_FENCE_GATE
                                || mat == Material.ACACIA_FENCE_GATE
                                || mat == Material.DARK_OAK_FENCE_GATE));
    }

    private boolean isCanUseEntity_RClickOnly(final Material mat, final Block block) {
        return mat == Material.ITEM_FRAME
                || mat == Material.BEACON
                || mat == Material.FLOWER_POT
                || mat == Material.COMMAND
                || mat == Material.ANVIL
                || mat == Material.CAKE_BLOCK
                || mat == Material.NOTE_BLOCK
                || mat == Material.DIODE
                || mat == Material.DIODE_BLOCK_OFF
                || mat == Material.DIODE_BLOCK_ON
                || mat == Material.BED_BLOCK
                || mat == Material.WORKBENCH
                || mat == Material.BREWING_STAND
                || mat == Material.ENCHANTMENT_TABLE
                || plugin.getConfigManager().getCustomRightClick().contains(block.getType());
    }

    private boolean isContainer(final Material mat, final Block block) {
        return FlagPermissions.getMaterialUseFlagList().containsKey(mat) && FlagPermissions.getMaterialUseFlagList().get(mat).equals("container") || plugin.getConfigManager().getCustomContainers().contains(block.getType());
    }
}
